const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const mysql = require('mysql')
const port = 3002


//创建数据库连接
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'hotel'
})

connection.connect()

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

//设置跨域访问
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  next();
});

//test
app.get('/', function (req, res) {
  res.send('hello world')
})

//城市酒店获取
app.get('/api/hotel_list', (req, res) => {
  let cityId = req.query.id
  connection.query(`select * from hotel_details where province_id=${cityId}`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

// 获取城市
app.get('/api/city_list', (req, res) => {
  connection.query(`select province,province_id from hotel_details;`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

// 酒店搜索
app.get('/api/hotel_all', (req, res) => {
  connection.query(`select * from hotel_details ;`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

//酒店详情
app.get('/api/hotel_detailInfo', (req, res) => {
  let id = req.query.id
  connection.query(`select * from hotel_details where hotel_id=${id};`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

// 注册
app.post('/api/register', (req, res) => {
  let user_name = req.body.user_name
  let user_phone = req.body.user_phone
  let user_password = req.body.user_password
  let user_avatar = req.body.user_avatar
  connection.query(`insert into users (user_name,user_phone,user_password,user_avatar) values ("${user_name}","${user_phone}","${user_password}","${user_avatar}")`, (err, rows, fields) => {
    res.send({
      status: 200,
    })
  })
})

// 手机号登录
app.post('/api/login_phone', (req, res) => {
  let phone = req.body.phone
  // console.log(phone);
  connection.query(`select * from users where user_phone="${phone}"`, (err, rows, fields) => {
    if (rows.length > 0) {
      res.send({
        status: 1,
      })
    } else {
      res.send({
        status: 0,
      })
    }
  })
})

// 密码登录
app.post('/api/login_pwd', (req, res) => {
  let phone = req.body.phone
  let pwd = req.body.password
  connection.query(`select * from users where user_phone="${phone}" and user_password="${pwd}"`, (err, rows, fields) => {
    if (rows.length > 0) {
      res.send({
        status: 1,
      })
    } else {
      res.send({
        status: 0,
      })
    }
  })
})

// 获取酒店的房间名字
app.get('/api/hotel_base_name', (req, res) => {
  connection.query(`select base_room_name from hotel_base_rooms;`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

// 获取酒店的房间
app.get('/api/hotel_base_rooms', (req, res) => {
  let name = req.query.name
  connection.query(`select * from hotel_base_rooms where base_room_name="${name}";`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

// 添加订单
app.post('/api/order', (req, res) => {
  let rooms_name = req.body.rooms_name
  let phone = req.body.phone
  let name = req.body.name
  let detail_info = req.body.detail_info
  let pay_status = req.body.pay_status
  let min_price = req.body.min_price
  // console.log(detail_info);
  connection.query(`insert into hotel_order (rooms_name,phone,name,detail_info,pay_status,min_price) values ("${rooms_name}","${phone}","${name}",'${detail_info}',"${pay_status}",${min_price})`, (err, rows, fields) => {
    res.send({
      status: 200,
    })
  })
})

// 获取订单
app.get('/api/getOrderInfo', (req, res) => {
  let phone = req.query.phone
  // console.log(phone);
  connection.query(`select * from hotel_order where phone="${phone}"`, (err, rows, fields) => {
    res.send({
      status: 200,
      data: rows
    })
  })
})

// 删除订单
app.post('/api/delete_order', (req, res) => {
  let id = req.body.id
  // console.log(phone);
  connection.query(`delete from hotel_order where order_id="${id}"`, (err, rows, fields) => {
    res.send({
      status: 200,
    })
  })
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}/`);
});
// connection.end()